package mq.rabbitmq.returnlistener;

import com.rabbitmq.client.*;
import mq.rabbitmq.Constant;
import mq.rabbitmq.RabbitMqChannelFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author verlet
 * @date 2019/12/29
 */
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Channel channel = RabbitMqChannelFactory.createChannel();
        channel.exchangeDeclare(Constant.RETURN_EX_CHANGE, BuiltinExchangeType.DIRECT,false,false,null);
        String queueName = "returnQueue";
        channel.queueDeclare(queueName,false,false,false,null);
        channel.queueBind(queueName,Constant.RETURN_EX_CHANGE,Constant.RETURN_ROUTING_KEY);

        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
            }
        };

        channel.basicConsume(queueName,true,consumer);
        Thread.sleep(1000);
        // 关闭连接
        RabbitMqChannelFactory.close(channel);
    }
}
